#ifndef MESSAGESHOWAREA_H
#define MESSAGESHOWAREA_H

#include <QWidget>
#include <QScrollArea>
#include <QStyle>
#include <QScrollBar>
#include <QVBoxLayout>
#include <QLabel>
#include <QPushButton>
#include <QStyleOption>
#include <QPainter>
#include <QTimer>
#include <QPainterPath>
#include <QPlainTextEdit>
#include "model/data.h"

using data::Message;
using data::MessageType;
using data::TEXT_TYPE;
using data::IMAGE_TYPE;
using data::FILE_TYPE;
using data::SPEECH_TYPE;

class MessageContentLabel : public QWidget
{
public:
    MessageContentLabel(MessageType type, const QString& text, const QString&
                        fileId, const QByteArray& content, bool isLeft);
    void paintEvent(QPaintEvent* ) override;
    void mousePressEvent(QMouseEvent* ) override;
    void contextMenuEvent(QContextMenuEvent* event) override;
private:
    QLabel* label;
    // 保存消息类型
    MessageType messageType;
    // ⽂件id
    QString fileId;
    // 保存消息内容(⽐如⽂件消息, 语⾳消息的⼆进制内容), 为后续的 "播放" / "另存为" 做准备
    QByteArray content;
    // 是否是左侧消息
    bool isLeft;
};

class MessageImageLabel : public QWidget
{

    Q_OBJECT
public:
    MessageImageLabel(const QString& fileId, const QByteArray& conten, bool isLeft);
    // TODO
};

// 表⽰⼀个消息条⽬
class MessageItem : public QWidget
{
    Q_OBJECT
public:
    MessageItem(bool isLeft);
    // 通过⼯⼚设计模式构造 Message
    static MessageItem* makeMessageItem(bool isLeft, const Message& message);
private:
    static QWidget* makeTextMessageItem(const QByteArray& content, bool
                                        isLeft);
    static QWidget* makeImageMessageItem(const QString& fileId, const
                                         QByteArray& content, bool isLeft);
    static QWidget* makeFileMessageItem(const QString& fileId, const
                                        QByteArray& content, bool isLeft, const QString& fileName);
    static QWidget* makeSoundMessageItem(const QString& fileId, const
                                         QByteArray& content, bool isLeft);
    bool isLeft;
};

class MessageShowArea : public QScrollArea
{
    Q_OBJECT
public:
    explicit MessageShowArea(QWidget *parent = nullptr);

    void addMessage(bool isLeft, const Message& message);
    // 新增⼀个 Message (头插)
    void addFrontMessage(bool isLeft, const Message& message);
    // 清空所有 Message
    void clear();
    // 设置滚动条滚动到末尾
    void scrollToEndLater();
signals:

private:
    QWidget* container;
};

class MessageEditArea : public QWidget
{
    Q_OBJECT
public:
    explicit MessageEditArea(QWidget* owner);
    // 消息输⼊框
    QPlainTextEdit* textEdit;
    // 提⽰信息 label
    QLabel* tipLabel;
    // 发送消息按钮
    QPushButton* sendBtn;
    // 发送图⽚消息
    QPushButton* sendImageBtn;
    // 发送⽂件消息
    QPushButton* sendFileBtn;
    // 显⽰历史消息按钮
    QPushButton* showHistoryBtn;
    // 发送语⾳按钮
    QPushButton* sendSoundBtn;
private:
    // 被哪个窗⼝持有. 指向 MainWidget 的指针
    QWidget* owner;
signals:
};

#endif // MESSAGESHOWAREA_H
